import { defineStore } from 'pinia'

export const useAppStore = defineStore('app', {
  state: () => ({
    sidebarCollapsed: false,
    visitedViews: []
  }),
  
  actions: {
    toggleSidebar() {
      this.sidebarCollapsed = !this.sidebarCollapsed
    },
    
    // 添加访问过的视图到标签页
    addVisitedView(view) {
      if (this.visitedViews.some(v => v.path === view.path)) return
      
      // 添加到已访问视图
      this.visitedViews.push(
        Object.assign({}, view, {
          title: view.title || 'no-name'
        })
      )
    },
    
    // 移除标签页
    removeVisitedView(path) {
      for (const [i, v] of this.visitedViews.entries()) {
        if (v.path === path) {
          this.visitedViews.splice(i, 1)
          break
        }
      }
    },
    
    // 清除所有标签页，除了仪表盘
    clearVisitedViews() {
      // 保留仪表盘标签
      const affixTags = this.visitedViews.filter(tag => tag.path === '/back/dashboard')
      this.visitedViews = affixTags
    }
  }
}) 